package org.lucky.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.lucky.admin.mapper.LuckyMapper;
import org.lucky.admin.service.ILuckyService;
import org.lucky_common.domain.ChartLine;
import org.lucky_common.domain.LuckyShare;
import org.lucky_common.domain.LuckyShareCount;
import org.lucky_common.domain.ParseParamObj;
import org.lucky_common.util.AjaxResult;
import org.lucky_common.vo.LuckyShareExportVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class LuckyServiceImpl extends ServiceImpl<LuckyMapper, LuckyShare> implements ILuckyService {

    @Resource
    private LuckyMapper luckyMapper;

    @Override
    public LuckyShare selectOneWithInvite(String inviteCode) {
        return luckyMapper.selectOneWithInvite(inviteCode);
    }

    @Override
    public List<LuckyShareExportVo> getUserExportList() {
        List<LuckyShare> users = this.list();
        //users集合转成export集合
        return users.stream().map(user -> {
            LuckyShareExportVo vo = new LuckyShareExportVo();
            BeanUtils.copyProperties(user, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public Map<String, List> getCount() {
        List<LuckyShareCount> count = luckyMapper.getLuckyShareCount();
        Map<String, List> longStringMap = new HashMap<>();
        List<Long> longs = new ArrayList<>();
        List<String> name = new ArrayList<>();
        name.add("完成的人数");
        longs.add((long) count.size());

        List<LuckyShareCount> count2 = luckyMapper.getLuckyShareCount2();
        name.add("未完成的人数");
        longs.add((long) count2.size());

        longStringMap.put("name",name);
        longStringMap.put("count",longs);
        return longStringMap;
    }

    @Override
    public List getChartLine() {
        List<ChartLine> chartLineList = luckyMapper.getChartLine();
        List<Object> assembledData = new ArrayList<>();
        List<String> dataName = new LinkedList<>();
        List<Integer> count = new LinkedList<>();
        chartLineList.stream().filter(i -> i.getDay_of_week() != null).forEach(i -> {
            dataName.add(i.getDay_of_week());
            count.add(i.getCount());
        });
        Map<Object, Object> name = new LinkedHashMap<>();
        Map<Object, Object> count2 = new LinkedHashMap<>();
        name.put("name",dataName);
        count2.put("count",count);
        assembledData.add(name);
        assembledData.add(count2);
        return assembledData;
    }

}
